<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

          <p-col :span="12">
            <p-form-item label="币别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'curid', validatorRules.curid]" placeholder="请输入币别"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="币别名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'curname', validatorRules.curname]" placeholder="请输入币别名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="本位币标志" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number v-decorator="[ 'localflg', validatorRules.localflg]" placeholder="请输入本位币标志" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="货币折算方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number v-decorator="[ 'changetyp', validatorRules.changetyp]" placeholder="请输入货币折算方式" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="固定浮动汇率标志" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number v-decorator="[ 'frateflg', validatorRules.frateflg]" placeholder="请输入固定浮动汇率标志" style="width: 100%"/>
            </p-form-item>
          </p-col>

        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="币别汇率" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="baCurrateTable.loading"
            :columns="baCurrateTable.columns"
            :dataSource="baCurrateTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
        <p-tab-pane tab="币别票面表" :key="refKeys[1]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[1]"
            :loading="baParvalueTable.loading"
            :columns="baParvalueTable.columns"
            :dataSource="baParvalueTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BaCurrencyModal',
    mixins: [JEditableTableMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          curid: {rules: [
            {required: true, message: '请输入币别!'},
          ]},
          curname: {rules: [
            {required: true, message: '请输入币别名称!'},
          ]},
          remark: {rules: [
          ]},
          localflg: {rules: [
          ]},
          changetyp: {rules: [
            {required: true, message: '请输入货币折算方式!'},
           {pattern:/^-?\d+\.?\d*$/, message: '请输入数字!'},
          ]},
          frateflg: {rules: [
          ]},
        },
        refKeys: ['baCurrate', 'baParvalue', ],
        tableKeys:['baCurrate', 'baParvalue', ],
        activeKey: 'baCurrate',
        // 币别汇率
        baCurrateTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '币别',
              key: 'curid',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '汇率日期',
              key: 'ratedt',
              type: FormTypes.date,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '会计年',
              key: 'years',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '会计期间',
              key: 'period',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '汇率',
              key: 'scale',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '调整汇率',
              key: 'adjustrate',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '期间结束日期标志',
              key: 'endtflg',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '币别名称',
              key: 'curname',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '期初汇率',
              key: 'currate',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        // 币别票面表
        baParvalueTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '币别',
              key: 'curid',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '币名称',
              key: 'parname',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '面值',
              key: 'parvalue',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: '/codedemoapi/feixuns/baCurrency/add',
          edit: '/codedemoapi/feixuns/baCurrency/edit',
          baCurrate: {
            list: '/codedemoapi/feixuns/baCurrency/queryBaCurrateByMainId'
          },
          baParvalue: {
            list: '/codedemoapi/feixuns/baCurrency/queryBaParvalueByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'curid','curname','remark','localflg','changetyp','frateflg')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.baCurrate.list, params, this.baCurrateTable)
          this.requestSubTableData(this.url.baParvalue.list, params, this.baParvalueTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          baCurrateList: allValues.tablesValue[0].values,
          baParvalueList: allValues.tablesValue[1].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'curid','curname','remark','localflg','changetyp','frateflg'))
     },

    }
  }
</script>

<style scoped>
</style>